window.onload = function () {
	//按钮和组件
	var thrown = document.querySelector('.throw');
	var onSale = document.querySelector('.on-sale');
	var onBtn = document.querySelector('nav>.item-wrap>span:first-of-type');
	var offBtn = document.querySelector('nav>.item-wrap>span:nth-of-type(2)');
	var addBtn = document.querySelector('.add');
	var manageBtn = document.querySelector('.manage');
	var finishBtn = document.querySelector('.search>span');
	var addPanel = document.querySelector('.btn-group');
	var managePanel = document.querySelector('.manage-panel');
	var front = document.querySelector('.front');
	
	var makeSure1 = document.querySelectorAll('.on-sale .make-sure');
	console.log(makeSure1);
	var makeSure2 = document.querySelectorAll('.throw .make-sure');
	console.log(makeSure2);
	//下架面板
	var throwProduct = thrown.querySelectorAll('.product');
	//上架面板
	var onSaleProduct = onSale.querySelectorAll('.product');
	
	var allCheckBox = document.querySelectorAll('input[type="checkbox"]');
	var throwCheckBox = thrown.getElementsByTagName('input');
	var onSaleCheckBox = onSale.getElementsByTagName('input');
	
	var arr = [onBtn,offBtn];
	var arrs = [addBtn,manageBtn];
	
	var index = 0;
	//隐藏菜单栏
	var barBtn = document.querySelectorAll('.bar-btn');
	var control = false;
	var gf 

	

//	var result;
	thrown.style.display = 'none';
	//点击“上架商品”效果
	onBtn.addEventListener('click', onBtnFunc );
	//点击“已下架”效果
	offBtn.addEventListener('click', offBtnFunc);
	//点击"添加商品"效果
	addBtn.addEventListener('click', function () {
		this.classList.add('status');
		manageBtn.classList.remove('status');
		setTimeout(function () {
			addBtn.classList.remove('status');
		},80);
	});
	//点击“批量管理”效果
	manageBtn.addEventListener('click', manageBtnFunc);
	//点击“完成”效果	
	finishBtn.addEventListener('click', function(){		
			managePanel.style.display = 'none';
			managePanel.style.bottom = '-100%';
			addPanel.style.display = 'block';
			for (var i = 0; i < makeSure1.length; i++ ) {
				makeSure1[i].style.display = 'none';				
			}
			
			for ( var j =0; j < makeSure2.length; j++ ) {
				makeSure2[j].style.display = 'none';
			}
			
			for ( var k = 0; k <　throwProduct.length; k++) {
				throwProduct[k].style.float = 'none';
			}
			
			for ( var l = 0; l < onSaleProduct.length; l++ ) {
				onSaleProduct[l].style.float = 'none';
			}
			finishBtn.style.display = 'none';
			for ( var i = 0; i < allCheckBox.length; i++ ){
				allCheckBox[i].checked = false;
			}
			index = 0;
	});
	

	
	function change ( val1, val2, val3, val4 ) {
		thrown.style.right = val1;
		thrown.style.display = val2;
		onSale.style.left = val3;
		onSale.style.display = val4;
	}
	
				
	
	//不同页面的不同全选
		
		for ( var i = 0; i < allCheckBox.length; i++ ){				
				allCheckBox[i].addEventListener('click', function () {
					if ( this.className == 'front' ) {		
						if ( thrown.style.display == 'none' ){
							for ( var j = 0; j < onSaleCheckBox.length; j++){
								onSaleCheckBox[j].checked = this.checked;
							}
														
						}
						else if ( thrown.style.display == 'block' ) {
							for ( var j = 0; j < throwCheckBox.length; j++){
								throwCheckBox[j].checked = this.checked;

							}
						}
						
					}
					
					
				});
		}
		//修复全选bug
		front.addEventListener('click', function() {
			if ( thrown.style.display == 'none' ){
				seletPatch(onSaleCheckBox.length);
			}
			else if ( thrown.style.display == 'block') {
				seletPatch(throwCheckBox.length);
			}
		} );
		
		
	
		function seletPatch( num ) {
			if( index >= num ) {
				index = 0;
			} else if ( index >= 0 ) {
				index = num;
			}
			console.log(index);
		}
		
		
		//升级已经上架全选
		for ( var k = 0; k < onSaleCheckBox.length; k++ ){
				onSaleCheckBox[k].addEventListener('click', function () {
					if ( this.checked == true ) {					
						index++;
						console.log(index);
												
					} else{
						index--;
						console.log(index);
					}
					
					if( index == onSaleCheckBox.length) {
						front.checked = true;	
						index = 0;
					} 
					else if ( index < onSaleCheckBox.length ){
						front.checked = false;
					}
						
				});
		}
		//升级判断已下架全选
			
		for ( var l = 0; l < throwCheckBox.length; l++ ){
				throwCheckBox[l].addEventListener('click', function () {
					
					if ( this.checked == true ) {					
						index++;
						console.log(index);
												
					} else if ( this.checked == false){
						index--;
						console.log(index);
					}
					
					if( index == throwCheckBox.length) {
						front.checked = true;
						index = 0;
					} 
					else if ( index < throwCheckBox.length ){
						front.checked = false;
					}
					
					
				});
		}

	
	 function onBtnFunc () {
	 	for ( var t = 0; t < throwCheckBox.length; t++ ) {
	  		if ( throwCheckBox[t].checked == true ) {
	  				return false;
	  		}
	  	}
	  	
	 	if (front.checked == true ){
	 		return false;
	 	} else {
	 		change('-100%','none','0','block');
			for ( var i = 0; i < arr.length; i++ ) {
				arr[i].className = '';
			}
			onBtn.className = 'active';
			offBtn.className = 'un-active';				
	 	}	
	 	var s = document.querySelector('.to-sale');
	 	var t = document.querySelector('.thrown');
		s.style.display = 'none';
		t.style.display = 'inline-block';	
		var d = document.querySelector('.delete');
		d.style.marginLeft = '0.5rem';
	}
	
	
	function offBtnFunc () {
		for ( var s = 0; s < onSaleCheckBox.length; s++ ) {
	  		if ( onSaleCheckBox[s].checked == true ) {
	  				return false;
	  		}
	  	}
		if (front.checked == true){
			return false;
		} else{
			change('0','block','-100%','none');
			for ( var i = 0; i < arr.length; i++ ) {
				arr[i].className = '';
			}
			offBtn.className = 'active';
			onBtn.className = 'un-active';		
		}
		var s = document.querySelector('.to-sale');
	 	var t = document.querySelector('.thrown');
	 	var d = document.querySelector('.delete');

		t.style.display = 'none';
		s.style.display = 'inline-block';	
		d.style.marginLeft = '4.5rem';
	} 
	
	function manageBtnFunc () {
		if (document.querySelector('.hide-bar').style.display = 'block' ) {
			 document.querySelector('.hide-bar').style.display = 'none';
			for ( var b = 0; b < barBtn.length; b++ ) {
				 barBtn[b].querySelector('img').src = 'img/product_img/product_01.png';
			}
		}
		this.classList.add('status');
		addBtn.classList.remove('status');
		setTimeout(function () {
			manageBtn.classList.remove('status');
		},30);
		setTimeout(function () {
			managePanel.style.display = 'block';
			managePanel.style.bottom = '0';
			addPanel.style.display = 'none';
			for (var i = 0; i < makeSure1.length; i++ ) {
				makeSure1[i].style.display = 'block';				
			}
			
			for ( var j =0; j < makeSure2.length; j++ ) {
				makeSure2[j].style.display = 'block';
			}
			
			for ( var k = 0; k <　throwProduct.length; k++) {
				throwProduct[k].style.float = 'right';
			}
			
			for ( var l = 0; l < onSaleProduct.length; l++ ) {
				onSaleProduct[l].style.float = 'right';
			}
			
		},30);
		
			finishBtn.style.display = 'block';
	}
	
	 
	 
	
	//点击显示隐藏菜单栏 
	for (　var b = 0; b < barBtn.length; b++ ) {
		barBtn[b].addEventListener('touchstart', function (e) {
			var fe = e.target
			bar(fe);
		});
	}

function bar (fe) {
				
			gf = fe.parentNode.parentNode.parentNode.parentNode;
			
			if ( finishBtn.style.display == 'block') {
				return false;
			}
				
			if ( control == false) {
				fe.src = 'img/product_img/product_04.png';
				gf.querySelector('.hide-bar').style.display = 'block';
				control = true;
				
			} else if ( control == true) {
				fe.src = 'img/product_img/product_01.png';
				gf.querySelector('.hide-bar').style.display = 'none';
				control = false;
			}
			setTimeout(function() {
				fe.src = 'img/product_img/product_01.png';
				gf.querySelector('.hide-bar').style.display = 'none';
			},3000);
}
	
	
	

	
	//上架弹框
	
	var salePrompt = document.querySelector('.sale-prompt');
	//上架按钮
	var sPromptBtn = document.querySelector('.to-sale');
	var thrownBtn = document.querySelector('.thrown');
	var promptInnerBtn = salePrompt.querySelector('span:last-of-type');
	var promptcancelBtn = salePrompt.querySelector('span:first-of-type');
	var promptSuccess = document.querySelector('.on-sale-success-wrap');
	var d = [];
	var numbers ;
	//显示选择弹框
	sPromptBtn.addEventListener('click', function () {
		salePrompt.style.display = 'block';	
	} );
	thrownBtn.addEventListener('click', function () {
		toOnSale(onSaleCheckBox, onSale, thrown);
	} );
	promptInnerBtn.addEventListener('click', function ( ) {
		toOnSale(throwCheckBox, thrown, onSale);
	} );
	
	function toOnSale ( goal,remove,append ) {
		
		for ( var o = 0; o < goal.length; o++){
			if( goal[o].checked == true ) {
				goal[o].checked = false;
				d.push(goal[o]);
			}
		}
		if ( d.length == 0 ) {
			setTimeout(function () {
				salePrompt.style.display = 'none';
			},1000);			
		} 
		else {
				
				index == 0;		
				if ( thrown.style.display == 'block' ) {
					promptSuccess.style.display='block';
				}
				salePrompt.style.display = 'none';
				setTimeout(function () {
					promptSuccess.style.display='none';
				},1000);
				
				for ( var f = 0; f < d.length; f++ ) {
				 		remove.removeChild(d[f].parentNode.parentNode);
				}
					
				for ( var a = 0; a < d.length; a++ ) {
					append.appendChild( d[a].parentNode.parentNode );
				}
				finishBtn.click();
				
//				numbers = 3 - d.length;
//				offBtn.innerHTML = '已下架(' + numbers + ')';
				d.length = 0 ;
//				numbers = 0;
			}
		

	}
	
	promptcancelBtn.addEventListener('click', function() {
		salePrompt.style.display = 'none';
	} );
	
	//删除效果
	var dlt = managePanel.querySelector('.delete');
	dlt.addEventListener ('click', function () {
		if ( thrown.style.display == 'none' ) {
			for ( var i = 0; i < onSaleCheckBox.length; i++ ) {
				if ( onSaleCheckBox[i].checked == true ) {
					onSale.removeChild(onSaleCheckBox[i].parentNode.parentNode);
					index--;
				}
			}
		}
		if ( thrown.style.display == 'block' ) {
			for ( var j = 0; j < throwCheckBox.length; j++ ) {
				if ( throwCheckBox[j].checked == true ) {
					thrown.removeChild(throwCheckBox[j].parentNode.parentNode);
					index--;
				}
			}
		}
		
		
	} );
	

//移到顶部
	var toTop = document.querySelector('.move-top');
	var toTopPrompt = document.querySelector('.toTop-success-wrap');
	var ttp ;
	var ttemp;
	
		toTop.addEventListener('click', function () {
			console.log(toTopPrompt);
			if ( thrown.style.display == 'none' ) {
				for ( var i = 0; i < onSaleCheckBox.length; i++ ) {
					if ( onSaleCheckBox[i].checked == true ) {
							ttp++;
							if ( ttp = 1 ) {
								ttemp = onSaleCheckBox[i].parentNode.parentNode;
							}	
							ttp = 0;
					}
				}
					
				onSale.insertBefore(ttemp,onSale.querySelector('.product-wrap'));
				toTopPrompt.style.display = 'block';
				setTimeout(function () {
					toTopPrompt.style.display = 'none';	
				},1000);
				
			}
			if ( thrown.style.display == 'block' ) {
				for ( var j = 0; j < throwCheckBox.length; j++ ) {
					if ( throwCheckBox[j].checked == true ) {
							ttp++;
							if ( ttp = 1 ) {
								ttemp = throwCheckBox[j].parentNode.parentNode;
							}	
							ttp = 0;
					}
				}
			
				thrown.insertBefore(ttemp,thrown.querySelector('.product-wrap'));
				toTopPrompt.style.display = 'block';
				setTimeout(function () {
					toTopPrompt.style.display = 'none';	
				},1000);
			}
		});
		
		
		
	//推广效果
	var spreadBtn = document.querySelectorAll('.hide-bar>li:nth-of-type(4)');
	var spreadBlack = document.querySelector('.spread-prompt-wrap');
	var spreandWhite = spreadBlack.querySelector('.white-wrap');
	var sWCancelBtn = spreandWhite.querySelector('.spread-cancel');
	for ( var sp = 0; sp < spreadBtn.length; sp++ ) {
		spreadBtn[sp].addEventListener('click', function () {
			spreadBlack.style.display = 'block';
			setTimeout(function () {
				spreandWhite.className = 'white-wrap white-animation';
			},130);
		} );
	}
	
	sWCancelBtn.addEventListener('click', function () {		
		spreandWhite.className = 'white-wrap close-white-animation';
		setTimeout(function () {
			spreadBlack.style.display = 'none';	
		},300);
	});
}

